package com.seven.mybatis.util;

import com.submail.config.AppConfig;
import com.submail.lib.MESSAGEXsend;
import com.submail.utils.ConfigLoader;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class BCMessageUtil {

    /** 和appid 配置一样 */
    private static String appid = "27885";
    private static String appkey = "fce6932d0aab511e2d1f2c2e397ed843";
    private static String signtype = "normal";
    private static String projectId = "2ryGf4";

    /**
    * 发送验证码
    * @param phoneNumber 要发送验证码的手机号
    * @Author: Billy Chen
    * @Date:15:33 2018/9/28
    *
    */
    public static String bc_sendMessageTo(String phoneNumber) {
        
        AppConfig config = ConfigLoader.createConfig(appid, appkey, signtype);
        MESSAGEXsend submail = new MESSAGEXsend(config);

        submail.addTo(phoneNumber);
        submail.setProject(projectId);

        String code = bc_getRandNum();

        submail.addVar("app56688", code);

        String response = "";

        try {
            response = submail.xsend();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return code;
    }

    /**
    *
    * 生成随机验证码
    * @Author: Billy Chen
    * @Date:
    *
    */
    public static  String bc_getRandNum() {

        String randNum = new Random().nextInt(1000000)+"";

        if (randNum.length()!= 6) {   //如果生成的不是6位数随机数则返回该方法继续生成

            return bc_getRandNum();
        }

        return randNum;
    }

}
